package com.controller.goods;

import com.base.AjaxRequest;
import com.base.AjaxResult;
import com.base.BaseController;
import com.base.MetaInfo;
import com.bean.WebsiteContent;
import com.bean.anno.LogAnno;
import com.github.pagehelper.PageHelper;
import com.mapper.mysql.WebsiteContentMapper;
import com.mapper.repository.mysql.WebsiteContentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.util.List;
import java.util.UUID;

/**
 * 衣物管理  设计到用户
 */
@RestController
@RequestMapping("goodsClothing")
public class GoodsClothingController extends BaseController {

    @Autowired
    private WebsiteContentMapper websiteMapper;

    @Autowired
    private WebsiteContentRepository websiteRepository;

    @PostMapping("list")
    public AjaxResult list(@RequestBody AjaxRequest<WebsiteContent> request){
        WebsiteContent website = request.getT();
        MetaInfo metaInfo = request.getMetaInfo();

        PageHelper.startPage(metaInfo.getPage(),metaInfo.getSize());
        List<WebsiteContent> list = websiteMapper.select(website);

        return success("查询成功",list,metaInfo);
    }

    @GetMapping("{id}")
    public AjaxResult getOne(@PathVariable("id") String id){
        WebsiteContent websiteContent = websiteRepository.getById(id);
        System.out.println(websiteContent.toString());
        return success("查询成功",websiteContent);
    }

    @PostMapping("add")
    public AjaxResult add(@RequestBody WebsiteContent website){
        int count = websiteRepository.countByLink(website.getLink());
        if(count>0){
            return error("新增的链接已存在");
        }
        String id = UUID.randomUUID().toString();
        website.setId(id);
        website.setCreatedate(LocalDate.now());
        websiteRepository.save(website);
        return success("新增成功");
    }

    @PostMapping("del")
    public AjaxResult del(@RequestBody WebsiteContent website){
        websiteRepository.delete(website);
        return success("删除成功");
    }

    @PostMapping("update")
    public AjaxResult update(@RequestBody WebsiteContent website){
        int count = websiteRepository.countByLinkAndIdIsNot(website.getLink(),website.getId());
        if(count>0){
            return  error("更新的链接已存在");
        }
        website.setUpdatedate(LocalDate.now());
        websiteRepository.save(website);
        return success("更新成功");
    }

    @Value("${website.img}")
    private String websiteImgFolder;

    @LogAnno(apiName = "上传图片")
    @PostMapping("uploadImg")
    public AjaxResult uploadImg(@RequestParam("file") MultipartFile file){
        System.out.println(file.getOriginalFilename());
        //System.out.println(file.getName());
        File folder = new File(websiteImgFolder);
        if(!folder.exists()){
            folder.mkdirs();
        }
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        System.out.println(suffix);
        String img = UUID.randomUUID().toString();
        File imgFile = new File(websiteImgFolder,img+ suffix);
        try {
            file.transferTo(imgFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return success("上传成功",img+suffix);
    }
}
